package scheduleFile;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import schedule.Event;
import schedule.Schedule;

/**
 * Permet de definir la maniere dont est parse un fichier XML
 * @author Stanislas KOBAK / Celine BARILLERE
 *
 */
public class ScheduleHandler extends DefaultHandler
{
	private Schedule schedule;
	private Event event;
	private boolean inSchedule;
	private boolean inName;
	private boolean inEvents;
	private boolean inEvent;
	private boolean inSubject;
	private boolean inPlace;
	private boolean inDate;
	private boolean inDuration;
	private boolean inPersons;
	private boolean inPerson;
	private StringBuffer buffer;

	/**
	 * Constructeur
	 */
	public ScheduleHandler()
	{
		super();
	}
	
	/**
	 * Recupere un emploi du temps
	 * @return un emploi du temps
	 */
	public Schedule getSchedule()
	{
		return schedule;
	}
	
	/**
	 * 
	 */
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		if(qName.equals("schedule"))
		{
			schedule = new Schedule();
			inSchedule = true;
		}
		else if(qName.equals("events"))
		{
			inEvents = true;
		}
		else if(qName.equals("event"))
		{
			event = new Event();
			inEvent = true;
		}
		else if(qName.equals("persons"))
		{
			inPersons = true;
		}
		else
		{
			buffer = new StringBuffer();
			
			if(qName.equals("name"))
			{
				inName = true;
			}
			if(qName.equals("subject"))
			{
				inSubject = true;
			}
			if(qName.equals("place"))
			{
				inPlace = true;
			}
			if(qName.equals("date"))
			{
				inDate = true;
			}
			if(qName.equals("duration"))
			{
				inDuration = true;
			}
			if(qName.equals("person"))
			{
				inPerson = true;
			}
		}
	}
	
	/**
	 * 
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if(qName.equals("schedule"))
		{
			inSchedule = false;
		}
		else if(qName.equals("events"))
		{
			inEvents = false;
		}
		else if(qName.equals("event"))
		{
			schedule.addEvent(event);
			inEvent = false;
		}
		else if(qName.equals("persons"))
		{
			inPersons = false;
		}
		else
		{
			if(qName.equals("name"))
			{
				schedule.setName(buffer.toString());
				inName = false;
			}
			
			else if(qName.equals("subject"))
			{
				event.setSubject(buffer.toString());
				inSubject = false;
			}
			else if(qName.equals("place"))
			{
				event.setPlace(buffer.toString());
				inPlace = false;
			}
			else if(qName.equals("date"))
			{
				DateTimeFormatter format = ISODateTimeFormat.dateTime();
				DateTime dateTime = format.parseDateTime(buffer.toString());
				event.setStartDate(dateTime);
				inDate = false;
			}
			else if(qName.equals("duration"))
			{
				event.setDuration(Integer.parseInt(buffer.toString()));
				inDuration = false;
			}
			else if(qName.equals("person"))
			{
				event.addPerson(buffer.toString());
				inPerson = false;
			}
			
			buffer = null;
		}
	}

	/**
	 * 
	 */
	public void characters(char[] chars, int start, int length) throws SAXException
	{
		String read = new String(chars, start,length);
		
		if(buffer != null) 
		{
			buffer.append(read);       
		}
	}
}
